/**
 * 
 */

$.extend({
    redirectPost: function(location, args) {
        var form = '';
        if(args != null) {
        	$.each( args, function( key, value ) {
                form += '<input type="hidden" name="'+key+'" value="'+value+'">';
            });
        }
        $('<form action="'+location+'" method="POST">'+form+'</form>').submit();
    }, 
    
    getCookie: function(cname) {
    	var ck = document.cookie;
    	var ckValue = "";
    	if(ck != null) {
    	    var name = cname + "=";
    	    var ca = ck.split(";");
    	    for(var i=0; i<ca.length; i++) {
    	        var c = ca[i].trim();
    	        if (c.indexOf(name) == 0) { 
    	        	ckValue = c.substring(name.length, c.length);
    	        }
    	    }
    	}
        return ckValue;
    },
    
    formDataToArray: function(formObj){
        var unindexedArray = formObj.serializeArray();
        var indexedArray = {};

        $.map(unindexedArray, function(n, i){
            indexedArray[n['name']] = n['value'];
        });

        return indexedArray;
    },
    
    formDataToJson: function(formObj){
        return JSON.stringify($.formDataToArray(formObj));
    },
    
    formatDate: function(date) {
    	var day = date.getDate();
    	if(day < 10) {
    		day = '0' + day;
    	}
    	
    	var month = date.getMonth() + 1;
    	if(month < 10) {
    		month = '0' + month;
    	}
    	
    	var year = date.getFullYear();

    	return day + '/' + month + '/' +  year;
    },
    
    formatTime: function(date, showSeconds, showMilliseconds) {
    	var res = "";
    	
    	var hour = date.getHours();
    	if(hour < 10) {
    		hour = '0' + hour;
    	}
    	res += hour;
    	
    	var min = date.getMinutes();
    	if(min < 10) {
    		min = '0' + min;
    	}
    	res += ":" + min;
    	
    	if(showSeconds) {
    		var sec = date.getSeconds();
        	if(sec < 10) {
        		sec = '0' + sec;
        	}
        	res += ":" + sec;
    	}
    	
    	if(showMilliseconds) {
    		var ms = date.getMilliseconds();
        	if(ms < 100) {
        		ms = '00' + ms;
        	} else if(ms < 10) {
        		ms = '0' + ms;
        	}
        	res += "," + ms;
    	}
    	return res;
    },
    
    formatDateTime: function(date, showSeconds, showMilliseconds) {
    	return $.formatDate(date) + ' ' + $.formatTime(date, showSeconds, showMilliseconds);
    },
    
    stringToDate: function(date) {
    	if(date != null) {
	    	var dateParts = date.split("/");
	    	var res = new Date(
	    					dateParts[2],
	    					dateParts[1] - 1,
	    					dateParts[0],
	    					0, 0, 0, 0
	    				);
	    	
	    	return res;
    	}
    },
    
    stringToTime: function(time) {
    	if(time != null) {
	    	var timeParts = time.split(",");
	    	var millis = timeParts[1];
	    	timeParts = timeParts[0].split(":");
	    	var res = new Date(
	    					0, 0, 0,
	    					timeParts[0],
	    					timeParts[1],
	    					timeParts[2],
	    					millis
	    				);
	    	
	    	return res;
    	}
    },
    
    stringToDateTime: function(dateTime) {
    	if(dateTime != null) {
	    	var dateTimeParts = dateTime.split(" ");
	    	var strDate = dateTimeParts[0];
	    	var strTime = dateTimeParts[1];
	    	var date = $.stringToDate(strDate);
	    	var time = $.stringToTime(strTime);
	    	date.setHours(time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
	    	return date;
    	}
    }

});